"ui";
let stg = storages.create("lianfa");
let a_1=stg.get("setting");
ui.layout(
    <frame>
        <vertical>
            <horizontal>
                <img id="exit" src="file://./res/ic_keyboard_backspace_black_48dp.png" w="40" h="40" gravity="center"/>
                <text text="QQ连发器"  textColor="#333333" textSize="27sp" typeface="serif" gravity="top|center" w="*" textStyle="bold"/>
            </horizontal>
        <Switch id="autoService" text="无障碍服务" checked="{{auto.service != null}}" padding="8 8 8 8" textSize="15sp" textSize="18sp"/>
            <vertical>
                <horizontal>
                    <text text="内容：" textSize="26" gravity="center_vertical"/>
                    <input id="in_1" hint="输入任意文本内容"w="*"gravity="left|top" h="150" bg="#d3d3d3" margin="0 5 5 0"/>
                </horizontal>
                <horizontal>
                    <text text="间隔时间：" textSize="26" />
                    <input id="in_2" hint="请输入刷屏间隔（秒）" inputType="number" w="*"/>
                </horizontal>
                <horizontal>
                    <text text="次数：" textSize="26" />
                    <input id="in_3" hint="发送次数[必填]" inputType="number" w="*"/>
                </horizontal>
            </vertical>
        </vertical>
        <button text="开始运行" layout_gravity="bottom" id="btn_start" style="Widget.AppCompat.Button.Colored"w="*"h="180px"/>
    </frame>
);
var mt;
if(a_1 != null){
    ui.in_1.setText(a_1.a);
    ui.in_2.setText(a_1.b);
    ui.in_3.setText(a_1.c);
}   

ui.exit.click(()=>{ui.finish();});
ui.btn_start.click(() => {
    if(auto.service == null) {
        toast("请先开启无障碍服务！");
        return;
    }
    let sendingText = ui.in_1.text();
    let counts = ui.in_3.text();
    let rest = ui.in_2.text();
    if (sendingText.length != 0 && counts.length != 0 && rest.length != 0) {
        start();
    } else {
        toast("请把内容补充完整");
    }
});
ui.autoService.on("check", function(checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if(checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if(!checked && auto.service != null){
        auto.service.disableSelf();
    }
});

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function() {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});
function startSending() {
    let sendingText = ui.in_1.text();
    let rest = ui.in_2.text();
    let counts = ui.in_3.text();
    stg.put("setting",{"a":sendingText,"b":rest,"c":counts});
    toast("现在打开QQ会自动刷屏");
    rest = rest * 1000;
    newText = sendingText;
    for (var i = 1; i <= counts; i++) {
        sleep(rest);
        className("EditText").findOne().setText(newText);
        text("发送").findOne().click();
    }
}

function stop() {
    if (mt) {
        mt.interrupt();
        threads.shutDownAll();
        setTimeout(function() {
            if (mt.isAlive()) {
                toast("停止失败");
                return;
            }
            toast("已停止");
            mt = null;
            ui.run(function() {
                ui.btn_start.text("启动");
            });
        }, 300);
    }
}

function start() {
    if (mt) {
        stop();
        return;
    }
    mt = threads.start(startSending);
    ui.run(function() {
        ui.btn_start.text("停止");
    });
}